#pragma once

#include <iostream>
#include <cstdio>
#include <ctime>
#include <string>
#include <vector>
#include <memory>
#include <sstream>
#include <chrono>
#include <atomic>
#include <random>
#include <iomanip>
#include "jsoncpp/json/json.h"
#include "jsoncpp/json/writer.h"
#include "jsoncpp/json/forwards.h"

namespace NightCord
{
#define LDBG 0
#define LINF 1
#define LERR 2

#define LDEFAULT LDBG

#define LOG(level, format, ...)                                                         \
    {                                                                                   \
        if (level >= LDEFAULT)                                                          \
        {                                                                               \
            time_t t = time(nullptr);                                                   \
            struct tm *lt = localtime(&t);                                              \
            char tmp[32] = {0};                                                         \
            strftime(tmp, 31, "%Y-%m-%d %T", lt);                                       \
            printf("[%s][%s:%d] " format "\n", tmp, __FILE__, __LINE__, ##__VA_ARGS__); \
        }                                                                               \
    }

#define DLOG(format, ...) LOG(LDBG, format, ##__VA_ARGS__);
#define ILOG(format, ...) LOG(LINF, format, ##__VA_ARGS__);
#define ELOG(format, ...) LOG(LERR, format, ##__VA_ARGS__);

    class JSON
    {
    public:
        static bool serialize(const Json::Value &root, std::string &str)
        {

            std::stringstream ss;
            // 先实例化一个工厂类
            Json::StreamWriterBuilder swb;

            // 再由工厂类对象来生产派生类对象
            std::unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter());
            int ret = sw->write(root, &ss);
            if (ret != 0)
            {
                ELOG("json serialize failed");
                return false;
            }
            str = ss.str();
            std::cout << std::endl;
            return true;
        }

        static bool unserialize(const std::string &str, Json::Value &root)
        {
            // 实例化工厂对象
            Json::CharReaderBuilder crb;
            // 生产派生类对象
            std::unique_ptr<Json::CharReader> cr(crb.newCharReader());

            std::string errs;

            bool ret = cr->parse(str.c_str(), str.c_str() + str.size(), &root, &errs);
            if (ret == false)
            {
                ELOG("josn unserialize failed : %s", errs.c_str());
                return false;
            }

            return true;
        }
    };

    class UUID
    {
    public:
        static std::string Uuid()
        {
            std::stringstream ss;
            // 1.构造一个机器随机数对象
            std::random_device rd;
            // 2.以机器随机数为种子构造伪随机数对象
            std::mt19937 generator(rd());
            // 3.构造限定数据范围的对象
            std::uniform_int_distribution<int> distribution(0, 255);
            // 4.生成8个随机数，按照特定格式组织成为16进制数字字符的字符串
            for (int i = 0; i < 8; i++)
            {
                if (i == 4 || i == 6)
                    ss << "-";
                ss << std::setw(2) << std::setfill('0') << std::hex << distribution(generator);
            }
            ss << "-";
            // 5.定义一个8字节序号，逐字节组织成为16进制数字字符的字符串
            static std::atomic<size_t> seq(1);
            size_t cur = seq.fetch_add(1);
            for (int i = 7; i >= 0; i--)
            {
                if (i == 5)
                    ss << "-";
                ss << std::setw(2) << std::setfill('0') << std::hex << ((cur >> (i * 8)) & 0xFF);
            }

            return ss.str();
        }
    };

};
